package com.qf.lanzhou.blog.api.controller;

import com.qf.lanzhou.blog.api.dao.FeedbackDao;
import com.qf.lanzhou.blog.api.dao.Impl.FeedbackDaolmpl;
import com.qf.lanzhou.blog.api.pojo.entity.FeedbackEntity;
import com.qf.lanzhou.blog.api.pojo.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author jack
 */
@WebServlet("/feedbackInsert.do")
public class FeedbackInsertServlet extends BaseServletPost {
    private FeedbackDao fb = new FeedbackDaolmpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("反馈消息接收保存，doPost");
        //接收参数,解析参数
        FeedbackEntity feedbackEntity = paramsToEntity(req, FeedbackEntity.class);
        System.out.println(feedbackEntity);
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user!=null){
            feedbackEntity.setUserName(user.getUserName());
        }
        //传入数据库
        int i = fb.insert(feedbackEntity);
        //返回成功提醒
        if (i==1){
            successResultWrite(feedbackEntity,resp);
            return;
        }
        else {
            String msg = "服务器繁忙";
            failedResultWrite(msg,resp);
        }
    }
}
